package com.dlyk.service.impl;

import com.dlyk.constant.Constants;
import com.dlyk.manager.RedisManager;
import com.dlyk.mapper.TActivityMapper;
import com.dlyk.model.TActivity;
import com.dlyk.model.TUser;
import com.dlyk.query.ActivityQuery;
import com.dlyk.query.BaseQuery;
import com.dlyk.service.ActivityService;
import com.dlyk.util.CacheUtils;
import com.dlyk.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/18 16:45:06
 * @version: 1.0
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private TActivityMapper tActivityMapper;

    @Resource
    private RedisManager redisManager;

    @Override
    public PageInfo<TActivity> getActivityByPage(Integer current, ActivityQuery activityQuery) {
        //设置PageHelper的分页参数
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TActivity> list = tActivityMapper.selectActivityByPage(activityQuery);
        //封装分页参数到PageInfo
        PageInfo<TActivity> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public int saveActivity(ActivityQuery activityQuery) {

        TActivity tActivity = new TActivity();

        BeanUtils.copyProperties(activityQuery,tActivity);

        tActivity.setCreateTime(new Date());//创建时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(activityQuery.getToken());
        tActivity.setCreateBy(loginUserId);//创建人
        return tActivityMapper.insertSelective(tActivity);
    }

    @Override
    public TActivity getActivityById(Integer id) {
        return tActivityMapper.selectDetailByPrimaryKey(id);
    }

    @Override
    public int updateActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();

        BeanUtils.copyProperties(activityQuery,tActivity);

        tActivity.setEditTime(new Date());//编辑时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(activityQuery.getToken());
        tActivity.setEditBy(loginUserId);//编辑人
        return tActivityMapper.updateByPrimaryKeySelective(tActivity);
    }

    @Override
    public int delActivityById(Integer id) {
        return tActivityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelActivityByIds(List<String> idList) {
        return tActivityMapper.deleteByIds(idList);
    }

    @Override
    public List<TActivity> getOngoingActivity() {
        return tActivityMapper.selectOngoingActivity();
    }

    @Override
    public List<TActivity> getActivityByAll() {
        //从redis中查
        //redis查不到，就从数据库查，并且把数据放入redis（5分钟过期）
        return CacheUtils.getCacheData(()->{
            //生产者，从缓存redis中查询数据
            return (List<TActivity>)redisManager.getValue(Constants.REDIS_ACTIVITY_KEY);
        },()->{
            //生产者，从mysql中查询数据
            return tActivityMapper.getActivityByAll();
        },(t)->{
            redisManager.setValue(Constants.REDIS_ACTIVITY_KEY,t);
        });

    }
}
